﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace Ams.Mvc.Bootstrap
{
    public static class BootstrapValidationExtensions
    {
        #region ValidationMessageFor
        public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
        {
            return ValidationMessageFor(helper, expression, null /* validationMessage */, new RouteValueDictionary());
        }
        public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string validationMessage)
        {
            return ValidationMessageFor(helper, expression, validationMessage, new RouteValueDictionary());
        }
        public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string validationMessage, object htmlAttributes)
        {
            return ValidationMessageFor(helper, expression, validationMessage, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
        public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string validationMessage, IDictionary<string, object> htmlAttributes)
        {
            var attributes = htmlAttributes ?? new Dictionary<string, object>();

            if (helper.Form != null)
                HtmlMod.AppendToAttributeList(ref attributes, "class", helper.Form.ValidationClasses);

            var html = ValidationExtensions.ValidationMessageFor(helper.Html, expression, validationMessage, htmlAttributes);
            return html;
        } 
        #endregion

        #region ValidationSummary
        public static MvcHtmlString ValidationSummary(this BootstrapHelper helper)
        {
            return ValidationSummary(helper, false /* excludePropertyErrors */);
        }
        public static MvcHtmlString ValidationSummary(this BootstrapHelper helper, bool excludePropertyErrors)
        {
            return ValidationSummary(helper, excludePropertyErrors, null /* message */);
        }
        public static MvcHtmlString ValidationSummary(this BootstrapHelper helper, string message)
        {
            return ValidationSummary(helper, false /* excludePropertyErrors */, message, (object)null /* htmlAttributes */);
        }
        public static MvcHtmlString ValidationSummary(this BootstrapHelper helper, bool excludePropertyErrors, string message)
        {
            return ValidationSummary(helper, excludePropertyErrors, message, (object)null /* htmlAttributes */);
        }
        public static MvcHtmlString ValidationSummary(this BootstrapHelper helper, string message, object htmlAttributes)
        {
            return ValidationSummary(helper, false /* excludePropertyErrors */, message, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
        public static MvcHtmlString ValidationSummary(this BootstrapHelper helper, bool excludePropertyErrors, string message, object htmlAttributes)
        {
            return ValidationSummary(helper, excludePropertyErrors, message, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
        public static MvcHtmlString ValidationSummary(this BootstrapHelper helper, string message, IDictionary<string, object> htmlAttributes)
        {
            return ValidationSummary(helper, false /* excludePropertyErrors */, message, htmlAttributes);
        }
        public static MvcHtmlString ValidationSummary(this BootstrapHelper helper, bool excludePropertyErrors, string message, IDictionary<string, object> htmlAttributes)
        {
            var html = ValidationExtensions.ValidationSummary(helper.Html, excludePropertyErrors, message, htmlAttributes);
            html = BootstrapAlertExtensions.AlertDanger(helper, html.ToString());
            return html;
        }
        #endregion
    }
}
